package com.piper.auth.controller;

import com.piper.auth.utils.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/auth")
public class LoginController {

    @Resource
    private JwtTokenUtil tokenUtil;

    @PostMapping("/login")
    public String login(@RequestParam("userId") String userId, @RequestParam("password") String password) {
        Map<String, Object> load = new HashMap<>();
        load.put("userId", userId);
        load.put("userName", "佚名");
        load.put("companyId", "");
        log.info("欢迎 {}:{} 登录系统", userId, load.get("userName"));
        return tokenUtil.generateToken(userId, load);
    }

    @RequestMapping("/logout")
    public void logout(@RequestParam("token") String token) {
        String userId = tokenUtil.getUserIdFromToken(token);
        String userName = tokenUtil.getPrivateClaimFromToken(token, "userName");
        log.info("恭送 {}:{} 退出系统", userId, userName);
    }

}
